package com.itheima.health.controller.backend;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.POIUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author hm
 * @version 1.0
 * @date Created in 2024/7/15 17:44
 */
@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {
    @Resource
    private OrderSettingService orderSettingService;
    /**
     * 批量导入预约设置信息
     */
    @PostMapping("/upload.do")
    public Result upload(MultipartFile excelFile) throws IOException { //1.上传并读取文件信息
        List<String[]> lists = POIUtils.readExcel(excelFile); //2.保存文件的每一行数据
        for(String[] list: lists){
            OrderSetting orderSetting = new OrderSetting(new Date(list[0]), Integer.parseInt(list[1]));//将每一个单元格保存到对象里
            orderSettingService.add(orderSetting);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }
    /**
     * 展示预约设置信息
     */
    @GetMapping("/getOrderSettingByMonth.do")
    public Result show(@DateTimeFormat(pattern = "yyyy-MM") Date month){
        List<OrderSetting> list = orderSettingService.getByDate(month);
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS, list);
    }
    /**
     * 实现预约设置
     */
    @PostMapping("editNumberByOrderDate.do")
    public Result update(@RequestBody OrderSetting orderSetting){
        OrderSetting byOrderDate = orderSettingService.findByOrderDate(orderSetting.getOrderDate());
        if (orderSetting.getNumber() < byOrderDate.getReservations()){
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
        orderSettingService.update(orderSetting);
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

}
